<?php

/*

Process Update Stock (mygallery.pro.php)

*/
ob_start();
$rank_check = 1;
include "global.inc.php";

$find_items2 = mysql_query("SELECT * FROM usergallerys_items WHERE owner = '$userid' AND game = '$game'");
while ($find_items = mysql_fetch_array($find_items2))
{
	$id = $find_items[id];

	if ($remove[$id] > 0)
	{
		if ($remove[$id] <= $find_items[stock])
		{
			if (($find_items[stock] - $remove[$id]) >= 0)
			{
				mysql_query("UPDATE usergallerys_items SET stock=$find_items[stock]-$remove[$id] WHERE id = '$id' AND owner = '$userid' AND game = '$game'");
			}
			if (($find_items[stock] - $remove[$id]) == 0)
			{
				mysql_query("DELETE FROM usergallerys_items WHERE id = '$id' AND owner = '$userid' AND game = '$game'");
			}

			$findItem3 = fetch("SELECT id,parts FROM items2 WHERE id = '$find_items[item_id]' AND game = '$game'");
			$y = 1;
			while ($y <= $remove[$id])
			{
				mysql_query("INSERT INTO usersitems2 (owner,item_id,parts_left,game) VALUES ('$userid','$findItem3[id]','$findItem3[parts]','$game')");
				$y++;
			}
		}
	}
}

header(error("mygallery.php?game=$game","Gallery stock has been updated!"));

?>
